/*
 * Project Beknyou
 * Copyright (c) 2010-2011 Saint Paul College, All Rights Reserved
 * Redistributions in source code form must reproduce the above
 * Copyright and this condition.
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 */
package com.benkyou.server;

import com.benkyou.common.messages.ChatClientMessage;
import com.jme3.network.HostedConnection;
import com.jme3.network.Message;
import com.jme3.network.MessageListener;
import com.jme3.network.Server;
import com.jme3.network.serializing.Serializer;

/**
 *
 * @author Austin Allman
 */
public class ChatServer {

    private Server server;

    public ChatServer(Server server) {
        this.server = server;
    }

    public void startServer() {
        Serializer.registerClass(ChatClientMessage.class);
        ChatServerListener csl = new ChatServerListener();
        server.addMessageListener(csl, ChatClientMessage.class);
    }
}

class ChatServerListener implements MessageListener<HostedConnection> {

    public ChatServerListener() {
    }

    public void messageReceived(HostedConnection source, Message m) {
        if (m instanceof ChatClientMessage) {
            ChatClientMessage message = (ChatClientMessage) m;
            source.getServer().broadcast(message);
        }
    }
}
